package cn.tedu.shoot;

import java.awt.*;
import java.util.Random;
import java.awt.image.BufferedImage;

public abstract class  FlyingObject {
	public static final int LIVE=0;//活着的
	public static final int DEAD=1;//死了的
	public static final int REMOVE=2;//删除的
	protected int state=LIVE;//当前状态（默认为活着的）
	
	protected int width;  //宽
	protected int height; //高
	protected int x;      //x坐标
	protected int y;      //y坐标
	public FlyingObject(int width,int height){
		this.width=width;
		this.height=height;
		Random rand=new Random();
		x=rand.nextInt(World.WIDTH-width);//0到（窗口宽-小敌机宽）之内的随机数
		y=-height;
		
	}
	public FlyingObject(int width,int height,int x,int y){
		this.width=width;
		this.height=height;
		this.x=x;
		this.y=y;
	}
	
	
	public abstract void step();
	/**获取对象的图片*/
	public abstract BufferedImage getImage();
	/**判断对象是否是活着的*/
	public boolean isLive() {
		return state==LIVE;//若当前状态为LIVE，则表示为活着的，返回true
	}
	public boolean isDead() {
		return state==DEAD;
	}
	public boolean isRemove() {
		return state==REMOVE;
	}
	
	public boolean isOutOfBounds() {
		return y>=World.HEIGHT;
	}
	/**检测敌人与子弹/英雄机的碰撞 this：敌人 other：子弹/英雄机*/
	public boolean isHit(FlyingObject other) {
		int x1=this.x-other.width;
		int x2=this.x+this.width;
		int y1=this.y-other.height;
		int y2=this.y+this.height;
		int x=other.x;
		int y=other.y;
		return x>=x1&&x<=x2&&y>=y1&&y<=y2;	
	}
	/**飞行物去死*/
	public void goDead() {
		state=DEAD;
	}
	public void paint(Graphics g) {
		g.drawImage(getImage(),x,y,null);
	}


}
